package com.changpay.zywy.ui.activity.count

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.freeze.FreezePeopleActivity
import com.changpay.zywy.ui.activity.serivce.SelectProduceActivity
import com.changpay.zywy.ui.dialog.SelectMonthDialog
import kotlinx.android.synthetic.main.activity_feed_back.*
import kotlinx.android.synthetic.main.activity_income_detail.*
import kotlinx.android.synthetic.main.activity_tai_jun_first.*
import kotlinx.android.synthetic.main.title_layout.*

class CountTaiJunFirstActivity : BaseActivity(), IView {
    private var member_id: String = ""
    private var goods_id: String = ""
    private var time: String = ""
    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun attachLayoutRes(): Int {
        return R.layout.activity_tai_jun_first
    }

    override fun initData() {

    }

    override fun initView() {

        tv_title.text = "台均统计"
        iv_back.setOnClickListener(onClickListener)
        tv_daili_name.setOnClickListener(onClickListener)
        tv_daili_xinghao.setOnClickListener(onClickListener)
        tv_daili_time.setOnClickListener(onClickListener)
        btn_submit.setOnClickListener(onClickListener)
    }

    override fun start() {

    }

    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.tv_daili_name -> {
                Intent(this, FreezePeopleActivity::class.java).run {
                    putExtra("type", 4)
                    startActivityForResult(this, 100)
                }
            }
            R.id.tv_daili_xinghao -> {
                if (member_id.isNotEmpty()) {
                    Intent(this, SelectProduceActivity::class.java).run {
                        putExtra("member_id", member_id) // 默认为自己
                        putExtra("gc_id",intent.getStringExtra("gc_id"))
                        putExtra("type", 4)
                        startActivityForResult(this, 200)
                    }
                } else {
                    showToast("请先选择代理商")
                }
            }
            R.id.tv_daili_time -> {
                val builder = SelectMonthDialog.Builder(this)
                builder.setOnDialogClickListener { v, month ->
                    time = month.substring(0, 4)  + month.substring(4, 6)
                    tv_daili_time.text = month.substring(0, 4) + "-" + month.substring(4, 6)
                }
                val build = builder.build<SelectMonthDialog>()
                build.isCancelable = true
                build.show(supportFragmentManager)
            }
            R.id.btn_submit -> {
                if (member_id.isEmpty()) {
                    showToast("请选择查询代理商")
                    return@OnClickListener
                }
                if (goods_id.isEmpty()) {
                    showToast("请先选择查询机具")
                    return@OnClickListener
                }

                if (time.isEmpty()) {
                    showToast("请先选择日期")
                    return@OnClickListener
                }
                Intent(this, CountTaiJunSecondActivity::class.java).run {
                    putExtra("member_id", member_id) // 默认为自己
                    putExtra("goods_id", goods_id)
                    putExtra("time", time)
                    startActivity(this)
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200 && requestCode == 100) {
            tv_daili_name.text = data?.getStringExtra("member_name")
            member_id = data?.getStringExtra("member_id")!!
        }

        if (resultCode == 300 && requestCode == 200) {
            tv_daili_xinghao.text = data?.getStringExtra("produce_easy_name")
            goods_id = data?.getStringExtra("produce_id")!!
        }
    }
}
